package mrusanov.ui.stats

import mrusanov.tournament.RoundRobinTournamentRound
import javax.swing.{JTable, JPanel}
import mrusanov.team.Team
import javax.swing.table.DefaultTableModel
import mrusanov.util.swing.NotEditableTableModel
import net.miginfocom.swing.MigLayout

class RoundRobinTournamentRoundPanel(round:RoundRobinTournamentRound, group:Int = 0) extends JPanel {
  private val groupTeams = round.groups(group)
  private val teamResults = round.roundRobinResults(groupTeams)
  private val resultsMap = teamResults.map(r => (r.team, r)).toMap
  private val table = new JTable()
  private val tableModel = new NotEditableTableModel()

  setLayout(new MigLayout("fill"))
  for(result <- teamResults.sorted.zipWithIndex) {
    tableModel.addRow(getRow(result._2 + 1, result._1.team))
  }
  table.setModel(tableModel)
  add(table, "grow, push")

  private def getRow(pos:Int, team:Team):Array[AnyRef] = {
    val result = resultsMap(team)
    Array(pos.toString, team.name, result.gamesCount.toString, result.victories.toString,
      result.defeats.toString, result.difference.toString)
  }
}
